// /////////////////////////////////// 
//  
//  
//     (c)Copyright 2001-2010 Xiaojun (David) Chen 
//        Authored and Owned Xiaojun (David) Chen 
//        All Right Reserved
//  
// /////////////////////////////////// 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Collections.ObjectModel;

namespace CCF20Model
{
    
    public class PermissionData
    {
        [ReadOnly(true)]
        public int PermissionID { get; set; }
        [DisplayName("权限")]
        public string Permission { get; set; }
        [DisplayName("权限描述")]
        public string Description { get; set; }

    }
        
    public class PermissionModel
    {
        CCF20Entities ccf20Ent;

        
        public void AddPermissionToRole(string roleName, string permissionList)
        {
            CCFPermission permission = ccf20Ent.CCFPermissions.Where(c => c.RoleName.Contains(roleName)).FirstOrDefault();
            if (permission != null)
            {
                ccf20Ent.CCFPermissions.DeleteObject(permission);
            }
            permission = new CCFPermission();
            permission.RoleName = roleName;
            permission.PermissionList = permissionList;
            ccf20Ent.CCFPermissions.AddObject(permission);
            ccf20Ent.SaveChanges();
        }
        public string GetPermissionForRole(string roleName)
        {
            CCFPermission permission = ccf20Ent.CCFPermissions.Where(c => c.RoleName.Contains(roleName)).FirstOrDefault();
            if (permission != null)
            {
                return permission.PermissionList;
            }
            else
            {
                return null;
            }
        }
        
        public PermissionModel()
        {
            ccf20Ent = new CCF20Entities();
        }
    }
}
